#include "xmlHandle.h"
#include "xmlConstant.h"


#define PRINT_ENTER_FUN \
{}
//qDebug("in xmlHandle::%s", __func__)

void xmlHandle::userListToXml(QByteArray & data, const QList<userInfo> & list)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gUserListTag);
    for (int i = 0; i < list.size(); ++i) {
        xml.writeStartElement(gUserInfoTag);
        xml.writeTextElement(gIpTag, list.at(i).getIp());
        xml.writeTextElement(gNameTag, list.at(i).getName());
        xml.writeEndElement();
    }
    xml.writeEndElement();
    xml.writeEndDocument();
}

void xmlHandle::loginToXml(QByteArray & data, const QString & name)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gLoginTag);
    xml.writeTextElement(gNameTag, name);
    xml.writeEndElement();
    xml.writeEndDocument();
}

void xmlHandle::logoutToXml(QByteArray & data)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gLogoutTag);
    xml.writeEndElement();
    xml.writeEndDocument();
}

void xmlHandle::pluseToXml(QByteArray & data)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gPluseTag);
    xml.writeEndElement();
    xml.writeEndDocument();
}

void xmlHandle::recordInfoToXml(QByteArray & data, const recordInfo & info)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gRecordInfoTag);
    xml.writeTextElement(gCodeTag, QString::number(info.getErrorCode()));
    xml.writeTextElement(gNameTag, info.getName());
    xml.writeTextElement(gContentTag, info.getContent());
    xml.writeEndElement();
    xml.writeEndDocument();
}

void xmlHandle::statusToXml(QByteArray & data, int code)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gStatusTag);
    xml.writeTextElement(gCodeTag, QString::number(code));
    xml.writeEndElement();
    xml.writeEndDocument();
}
void xmlHandle::cmdToXml(QByteArray &data, QString &cmd, QByteArray &arg)
{
    PRINT_ENTER_FUN;
    data.clear();
    QXmlStreamWriter xml(&data);

    xml.writeStartDocument();
    xml.writeStartElement(gCmdTag);
    xml.writeTextElement(gFuncTag,QString(cmd));
    xml.writeTextElement(gArgsTag,QString(arg));
    xml.writeEndElement();
    xml.writeEndDocument();
}
void xmlHandle::xmlToCmd(const QByteArray &data, QString &cmd, QByteArray &arg)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(data);
    while (!xml.atEnd()) 
    {
        xml.readNext();
        if (xml.isStartElement()) 
        {
            if (gFuncTag == xml.name()) 
            {
                cmd = xml.readElementText();
            }
            if(gArgsTag == xml.name())
            {
                arg = xml.readElementText().toUtf8();
            }
        }
    }
}
void  xmlHandle::packetArgs(QByteArray &data, ...)
{

}
void xmlHandle::parseArgs(QByteArray &data,int &argc, char** argv)
{

}
void xmlHandle::packetArgs1(QByteArray &xmldata,int &code)
{
    PRINT_ENTER_FUN;
    xmldata.clear();
    QXmlStreamWriter xml(&xmldata);

    xml.writeStartDocument();
    xml.writeTextElement(gIntTypeTag,QString("%1").arg(code));
    xml.writeEndDocument();

}
void xmlHandle::packetArgs2(QByteArray &xmldata,QByteArray & data)
{
    PRINT_ENTER_FUN;
    xmldata.clear();
    QXmlStreamWriter xml(&xmldata);

    xml.writeStartDocument();
    xml.writeTextElement(gQByteArrayTypeTag,data.data());
    xml.writeEndDocument();

}
void xmlHandle::packetArgs3(QByteArray & xmldata,const QString & ip, const QByteArray & data)
{
    PRINT_ENTER_FUN;
    xmldata.clear();
    QXmlStreamWriter xml(&xmldata);

    xml.writeStartDocument();
    xml.writeStartElement(gRecordInfoTag);
    xml.writeTextElement(gQStringTypeTag,ip);
    xml.writeTextElement(gQByteArrayTypeTag,data.data());
    xml.writeEndElement();
    xml.writeEndDocument();

}
void xmlHandle::packetArgs4(QByteArray & xmldata,const QString & ip, int &code)
{
    PRINT_ENTER_FUN;
    xmldata.clear();
    QXmlStreamWriter xml(&xmldata);

    xml.writeStartDocument();
    xml.writeStartElement(gRecordInfoTag);
    xml.writeTextElement(gQStringTypeTag,ip);
    xml.writeTextElement(gIntTypeTag,QString("%1").arg(code));
    xml.writeEndElement();
    xml.writeEndDocument();

}
void xmlHandle::parseArgs1(QByteArray &xmldata,int &code)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(xmldata);
    while (!xml.atEnd()) 
    {
        xml.readNext();
        if (xml.isStartElement()) 
        {
            if (gIntTypeTag == xml.name()) 
            {
                code = xml.readElementText().toInt();
            }
        }
    }

}
void xmlHandle::parseArgs2(QByteArray &xmldata,QByteArray & data)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(xmldata);
    while (!xml.atEnd()) 
    {
        xml.readNext();
        if (xml.isStartElement()) 
        {
            if (gQByteArrayTypeTag == xml.name()) 
            {
                data = xml.readElementText().toAscii();
                //printf("parseArgs2:%s\n",data.data());
            }
        }
    }

}
void xmlHandle::parseArgs3(QByteArray & xmldata,QString & ip, QByteArray & data)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(xmldata);
    printf("parseArgs3:xmldata:%s\n",xmldata.data());
    while (!xml.atEnd()) 
    {
        xml.readNext();
        //printf("parseArgs3:ele name %s\n",xml.name().toString().toAscii().data());
        if (xml.isStartElement()) 
        {
            if (gQStringTypeTag == xml.name()) 
            {
                ip = xml.readElementText();
            }
            else if(gQByteArrayTypeTag == xml.name())
            {
                data = xml.readElementText().toAscii();
                //printf("parseArgs3:qbytearray:%s\n",data.data());
            }
        }
        
    }

}
void xmlHandle::parseArgs4(QByteArray & xmldata,QString & ip, int &code)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(xmldata);
    while (!xml.atEnd()) 
    {
        xml.readNext();
        if (xml.isStartElement()) 
        {
            if (gQStringTypeTag == xml.name()) 
            {
                ip = xml.readElementText();
            }
            else if(gIntTypeTag == xml.name())
            {
                code = xml.readElementText().toInt();
            }
        }
        
    }

}

void xmlHandle::read(const QString & ip, const QByteArray & data)
{
    PRINT_ENTER_FUN;
    QXmlStreamReader xml(data);

    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (gUserListTag == xml.name()) {
                emit sendUserList(xmlToUserList(xml));
                break;
            } else if (gPluseTag == xml.name()) {
                //server handle pluse
                emit sendPulse(ip);
                break;
            } else if (gRecordInfoTag == xml.name()) {
                emit sendRecordInfo(ip, xmlToRecordInfo(xml));
                break;
            } else if (gLoginTag == xml.name()) {
                //server handle login
                emit sendLogin(ip, xmlToLogin(xml));
                break;
            } else if (gLogoutTag == xml.name()) {
                //server handle logout
                emit sendLogout(ip);
                break;
            } else if (gStatusTag == xml.name()) {
                emit sendStatus(ip, xmlToStatus(xml));
            }
        }
    }
}

QList<userInfo> xmlHandle::xmlToUserList(QXmlStreamReader & xml)
{
    PRINT_ENTER_FUN;
    userInfo node;
    QList<userInfo> list;

    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (xml.name() == gIpTag) {
                node.setIp(xml.readElementText());
            } else if (xml.name() == gNameTag) {
                node.setName(xml.readElementText());
            }
        } else if (xml.isEndElement()) {
            if (gUserInfoTag == xml.name()) {
                list.append(node);
            } else if (gUserListTag == xml.name()) {
                break;
            }
        }
    }

    return list;
}


recordInfo xmlHandle::xmlToRecordInfo(QXmlStreamReader & xml)
{
    PRINT_ENTER_FUN;
    recordInfo info;
    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (xml.name() == gCodeTag) {
                bool ok = false;
                int code = xml.readElementText().toInt(&ok);
                if (ok) {
                    info.setErrorCode(code);
                } else {
                    return recordInfo();
                }
            } else if (xml.name() == gNameTag) {
                info.setName(xml.readElementText());
            } else if (xml.name() == gContentTag) {
                info.setContent(xml.readElementText());
            }
        } else if (xml.isEndElement()) {
            if (gRecordInfoTag == xml.name()) {
                break;
            }
        }
    }
    return info;
}

QString xmlHandle::xmlToLogin(QXmlStreamReader & xml)
{
    PRINT_ENTER_FUN;
    QString name;
    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (xml.name() == gNameTag) {
                name =xml.readElementText();
            }
        } else if (xml.isEndElement()) {
            if (gLoginTag == xml.name()) {
                break;
            }
        }
    }

    return name;
}

int xmlHandle::xmlToStatus(QXmlStreamReader & xml)
{
    PRINT_ENTER_FUN;
    int code;
    while (!xml.atEnd()) {
        xml.readNext();
        if (xml.isStartElement()) {
            if (xml.name() == gCodeTag) {
                code = xml.readElementText().toInt();
            }
        } else if (xml.isEndElement()) {
            if (gCodeTag == xml.name()) {
                break;
            }
        }
    }

    return code;
}
